const Koa=require('koa')
const koaBody= require('koa-body')
const koaStatic=require('koa-static')
const error=require('koa-json-error')
const parameter=require('koa-parameter')
const mongoose=require('mongoose')
const path=require('path')
const app=new Koa()  //引入并实例化Koa
const routing=require('./routes')
const {connectionStr}=require('./config')

mongoose.connect(connectionStr,{useNewUrlParser:true},()=>console.log(`mongoDB连接成功`))
mongoose.connection.on('error',console.error)

app.use(koaStatic(path.join(__dirname,'public')))
app.use(error({
    postFormat:(e,{stack,...rest})=>process.env.NODE_ENV==='production'?rest:{stack,...rest}
}))
app.use(koaBody({
    multipart:true,//启用文件
    formidable:{
        uploadDir:path.join(__dirname,'./public/uploads'),
        keepExtensions:true,
    }
}))
app.use(parameter(app))  //放在bodyparser后面
routing(app)

app.listen(3000,()=>console.log(`启动成功，3000端口`))





